//
//  RBVideoBrightnessView.m
//  RBVideoPlayer
//
//  Created by Lan on 2019/2/26.
//  Copyright © 2019 SummerTea. All rights reserved.
//

#import "RBVideoBrightnessView.h"

@interface RBVideoBrightnessView()

@property(nonatomic, strong) UILabel *topTextL;
@property(nonatomic, strong) UIImageView *centerImgView;
@property(nonatomic, strong) UIView *bottomContainerView;

@end

@implementation RBVideoBrightnessView

#pragma mark - Public

- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    self.frame = CGRectMake(0, 0, 155, 155);
    self.center = CGPointMake(self.superview.frame.size.width/2, self.superview.frame.size.height/2);
    self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
    self.layer.cornerRadius = 10;
    self.clipsToBounds = YES;
    self.topTextL.hidden = NO;
    self.centerImgView.hidden = NO;
    self.bottomContainerView.hidden = NO;
}

- (void)hide
{
    [UIView animateWithDuration:0.2 delay:0.2 options:0 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)setBrightness: (CGFloat)brightness
{
    NSArray *cubes = self.bottomContainerView.subviews;
    NSInteger count = cubes.count*brightness;
    count = MAX(count, 0);
    count = MIN(count, cubes.count);
    for (NSInteger i = 0; i < cubes.count; i++)
    {
        UIView *cube = cubes[i];
        cube.hidden = count < i;
    }
}

#pragma mark - Getter

- (UILabel *)topTextL
{
    if (!_topTextL)
    {
        self.topTextL = ({
            UILabel *view = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, self.frame.size.width, 30)];
            view.textColor = [UIColor colorWithRed:0.25 green:0.22 blue:0.21 alpha:1];
            view.textAlignment = NSTextAlignmentCenter;
            view.text = @"亮度";
            [self addSubview:view];
            view;
        });
    }
    return _topTextL;
}

- (UIImageView *)centerImgView
{
    if (!_centerImgView)
    {
        self.centerImgView = ({
            UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 79, 76)];
            view.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
            view.image = [UIImage imageNamed:@"RBMediaPlayer.bundle/brightness"];
            [self addSubview:view];
            view;
        });
    }
    return _centerImgView;
}

- (UIView *)bottomContainerView
{
    if (!_bottomContainerView)
    {
        self.bottomContainerView = ({
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(13, 132, self.frame.size.width - 26, 7)];
            view.backgroundColor = [UIColor colorWithRed:65.0/255.0 green:67.0/255.0 blue:70.0/255.0 alpha:1.0];
            [self addSubview:view];
            {
                NSInteger count = 16;
                CGFloat x = 0;
                CGFloat y = 1;
                CGFloat w = (view.frame.size.width - count - 1)/count;
                CGFloat h = view.frame.size.height - 2;
                for (NSInteger i = 0; i < count; i++)
                {
                    x = 1 + (w + 1)*i;
                    UIView *cube = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
                    cube.backgroundColor = [UIColor whiteColor];
                    [view addSubview:cube];
                }
            }
            view;
        });
    }
    return _bottomContainerView;
}

@end
